/*
 * BBMOpenChatMessagingServiceListener.java
 * 
 *
 * Research In Motion Limited proprietary and confidential
 * Copyright Research In Motion Limited, 2011-2011
 * 
 * Kyle Fowler
 * ALL KAPPS Software 2011
 */
package com.allkapps.bbmopenchat;

import net.rim.blackberry.api.bbm.platform.io.BBMPlatformChannel;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformChannelListener;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformOutgoingJoinRequest;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener;
import net.rim.blackberry.api.bbm.platform.io.IOErrorCode;
import net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact;
import net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener;
import net.rim.device.api.ui.component.Status;

import com.allkapps.bbmopenchat.BaseScreen;
import com.allkapps.bbmopenchat.utils.*;

/**
 * An implementation of the message service listener that handles channel, session,
 * and transfer events.
 * <p>
 * This listener is called when the current user accepts an invitation to a channel
 * or session.
 * <p>
 * This listener is registered with the
 * {@link net.rim.blackberry.api.bbm.platform.service.MessagingService MessagingService}
 *
 */
public class BBMOpenChatMessagingServiceListener extends MessagingServiceListener {

	private BaseScreen screen = null;
	
	public BBMOpenChatMessagingServiceListener(BaseScreen screen)
	{
		super();
		this.screen = screen;
	}
	
	public BaseScreen getScreen()
	{
		return screen;
	}
    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#channelCreated(net.rim.blackberry.api.bbm.platform.io.BBMPlatformChannel)
     */
    public void channelCreated(BBMPlatformChannel channel) {
        // This method is called when a contact accepts an invitation to a channel.
        // The listener is setup in getChannelListener
        channelCreated(channel, BBMBridge.MENU_ITEM_ID_NONE);
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#channelCreated(net.rim.blackberry.api.bbm.platform.io.BBMPlatformChannel, int)
     */
    public void channelCreated(BBMPlatformChannel channel, int menuItemId) {
        // This method is called when a new channel has been created to send the invitation associated with a menu item.        
        BBMBridge.getInstance().addConnection( channel );
        
        if (menuItemId != BBMBridge.MENU_ITEM_ID_NONE) {
            String channelDescription = BBMBridge.getInstance().getChannelDescription( menuItemId );
            if (channelDescription != null) {
            }
            if(screen != null) {
            	screen.updateScreen(BaseScreen.CHANNEL_CREATED, channel);
            }
        }
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#fileTransferFailed(java.lang.String, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, int)
     */
    public void fileTransferFailed(String path, BBMPlatformContact contact, int code) {
        Status.show("File transfer failed: " + BBMUtils.translateIOErrorCode(code));
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#getChannelListener(net.rim.blackberry.api.bbm.platform.io.BBMPlatformChannel)
     */
    public BBMPlatformChannelListener getChannelListener(BBMPlatformChannel channel) {
        // This method is called before channelCreated(channel)
        return new BBMOpenChatChannelListener(screen);
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#getSessionListener(net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession)
     */
    public BBMPlatformSessionListener getSessionListener(BBMPlatformSession session) {
        // This method is called before sessionCreated(Session)
    	BBMOpenChatSessionListener sess = new BBMOpenChatSessionListener();
    	sess.setScreen(screen);
        return sess;
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#sessionCreated(net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession)
     */
    public void sessionCreated(BBMPlatformSession session) {
        // This method is called when a contact accepts an invitation to a session.
        // The listener is setup in getSessionListener
        BBMOpenChatSessionListener sessionListener = (BBMOpenChatSessionListener) session.getListener();
        if (sessionListener != null) {
            session.setPublic();
            BBMBridge.getInstance().addConnection( session );
            if(screen != null){
            	sessionListener.setScreen(screen);
            	screen.updateScreen(BaseScreen.SESSION_CREATED, session);
            } else {
            	
            }
        }
        
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#sessionEnded(net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession)
     */
    public void sessionEnded(BBMPlatformContact contact, BBMPlatformSession session) {
        BBMOpenChatSessionListener sessionListener = (BBMOpenChatSessionListener) session.getListener();
        if (sessionListener != null) {
            sessionListener.sessionEnded();
        }
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#joinRequestAccepted(net.rim.blackberry.api.bbm.platform.io.BBMPlatformOutgoingJoinRequest, java.lang.String)
     */
    public void joinRequestAccepted( BBMPlatformOutgoingJoinRequest request, String cookie ) {
        super.joinRequestAccepted( request, cookie );
        if(screen != null){
        	screen.updateScreen(BaseScreen.INVITE_ACCEPTED);
        } else {
        	
        }
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.service.MessagingServiceListener#joinRequestDeclined(net.rim.blackberry.api.bbm.platform.io.BBMPlatformOutgoingJoinRequest, int)
     */
    public void joinRequestDeclined( BBMPlatformOutgoingJoinRequest request, int reasonCode ) {
        super.joinRequestDeclined( request, reasonCode );
        String reason = "";
        switch (reasonCode) {
            case IOErrorCode.JOIN_REQUEST_DECLINED_APPLICATION_NOT_RUNNING:
            	reason = "JOIN_REQUEST_DECLINED_APPLICATION_NOT_RUNNING";
                break;
            case IOErrorCode.JOIN_REQUEST_DECLINED_BY_HOST:
            	reason = "JOIN_REQUEST_DECLINED_BY_HOST";
                break;
            case IOErrorCode.JOIN_REQUEST_DECLINED_INVALID_HOST_PPID:
            	reason = "JOIN_REQUEST_DECLINED_INVALID_HOST_PPID";
                break;
            case IOErrorCode.JOIN_REQUEST_DECLINED_PUBLIC_CONNECTION_IS_FULL:
            	reason = "JOIN_REQUEST_DECLINED_PUBLIC_CONNECTION_IS_FULL";
                break;
            case IOErrorCode.JOIN_REQUEST_DECLINED_PUBLIC_CONNECTION_NOT_FOUND:
            	reason = "JOIN_REQUEST_DECLINED_PUBLIC_CONNECTION_NOT_FOUND";
                break;
        }
        if(screen != null) {
        	screen.updateScreen(BaseScreen.INVITE_REJECTED, reason);
        }
    }
    
}
